/*
 * $$Id: ForumLog.java,v 1.4 2004/01/26 11:32:05 bel70 Exp $$
 *
 * ***** BEGIN LICENSE BLOCK *****
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in 
 * compliance with the License. You may obtain a copy of the License 
 * at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and 
 * limitations under the License.
 *
 * The Original Code is jforum forum code.
 *
 * The Initial Developer of the Original Code is the JResearch, Org. 
 * Portions created by the Initial Developer are Copyright (C) 2004 
 * the Initial Developer. All Rights Reserved. 
 * 
 * Contributor(s): 
 *              Dmitry Belov <bel@jresearch.org>
 *        
 * ***** END LICENSE BLOCK ***** */
/*
 * Created on Oct 14, 2003
 *
  */
package com.jred.jforum.log;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.impl.Jdk14Logger;
import org.apache.commons.logging.impl.Log4JLogger;

import java.io.IOException;

import java.lang.reflect.Method;

import java.util.Properties;
import java.util.logging.FileHandler;
import java.util.logging.SimpleFormatter;


/**
 * DOCUMENT ME!
 *
 * @author Bel
 */
public class ForumLog {
    private static ForumLog ourInstance;
    private Log forumLogger;

    private ForumLog() {
        forumLogger = LogFactory.getLog("FORUM");

        Properties prop = new Properties();

        if (forumLogger instanceof Log4JLogger) {
            try {
                prop.load(getClass().getClassLoader().getResourceAsStream("log4j.properties"));

                Method configure = Class.forName(
                        "org.apache.log4j.PropertyConfigurator")
                                        .getDeclaredMethod("configure",
                        new Class[] { Properties.class });
                Object Configurator = Class.forName(
                        "org.apache.log4j.PropertyConfigurator").newInstance();
                configure.invoke(Configurator, new Object[] { prop });
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (forumLogger instanceof Jdk14Logger) {
            try {
                prop.load(getClass().getClassLoader().getResourceAsStream("org/jresearch/gossip/resources/jdk14log.properties"));

                Object handler = null;

                if (prop.getProperty("logs.type").equalsIgnoreCase("f")) {
                    handler = new FileHandler(prop.getProperty("file"),
                            Integer.parseInt(prop.getProperty("limit")),
                            Integer.parseInt(prop.getProperty("count")), true);
                    ((FileHandler) handler).setFormatter(new SimpleFormatter());
                    ((Jdk14Logger) forumLogger).getLogger().addHandler((FileHandler) handler);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * Method <code>getInstance</code> return instance
     *
     * @return instance
     */
    public synchronized static ForumLog getInstance() {
        if (ourInstance == null) {
            ourInstance = new ForumLog();
        }

        return ourInstance;
    }

    /**
     * Method <code>getForumLogger</code> return forum logger
     *
     * @return forum logger
     */
    public Log getForumLogger() {
        return forumLogger;
    }
}
